package com.xchen.imweb.message;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.IOException;
import java.util.List;

public class Message {

    /**
     * 给指定人发消息
     */
    public static void send(String username, String message) throws IOException {
        Session session = WebSocketSession.getSession(username);
        if(null != session) {
            session.getBasicRemote().sendText(message);
        }
    }

    public static void send(Session session, String message) throws IOException {
        if(null != session) {
            session.getBasicRemote().sendText(message);
        }
    }

    /**
     * 群发
     */
    public static synchronized void groupSend(HttpSession httpSession, String message) throws IOException {
        List<Session> sessionList = WebSocketSession.getSessionList();
        for (Session session : sessionList) {
            session.getBasicRemote().sendText(message);
        }
    }

}
